//
//  NRWFinancialCenterVC.m
//  Finance
//
//  Created by 邢毅 on 2018/7/31.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWFinancialCenterVC.h"
#import "NRWCenterCell.h"
#import "NRWCenterHeader.h"
#import "NRWApplyMortgageVC.h"

@interface NRWFinancialCenterVC ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (strong, nonatomic) NSArray<NSArray <NSDictionary *>*> *pageDataSource;

@property (strong, nonatomic) NSArray<NSString *> *pageHeaderDataSource;

@end

@implementation NRWFinancialCenterVC
{
	UICollectionView *_collectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.navigationView.title = @"金融中心";
	
	[self setupUI];
}

- (void)setupUI {
	
	UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
	layout.minimumLineSpacing = 0;
	layout.minimumInteritemSpacing = 0;
	layout.itemSize = CGSizeMake(SCREEN_WIDTH() / 3, 140);
	layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH(), 21);
	
	_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, getNavHeight(), SCREEN_WIDTH(), SCREEN_HEIGHT() - getNavHeight()) collectionViewLayout:layout];
	_collectionView.backgroundColor = [UIColor whiteColor];
	_collectionView.delegate = self;
	_collectionView.dataSource = self;
	_collectionView.contentInset = UIEdgeInsetsMake(15, 0, 0, 0);
	[_collectionView registerClass:NRWCenterCell.class forCellWithReuseIdentifier:@"cell"];
	[_collectionView registerClass:NRWCenterHeader.class forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
	[self.view addSubview:_collectionView];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
	
	return self.pageHeaderDataSource.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
	
	return self.pageDataSource[section].count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
	
	NRWCenterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
	
	cell.imageName = self.pageDataSource[indexPath.section][indexPath.item][@"imageName"];
	cell.titleString = self.pageDataSource[indexPath.section][indexPath.item][@"title"];
	
	return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
	
	if (kind == UICollectionElementKindSectionHeader) {
		
		NRWCenterHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
		
		header.headerTitle = self.pageHeaderDataSource[indexPath.item];
		
		return header;
	}
	
	return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
	
	if (indexPath.section == 0) {
		if (indexPath.item == 0) {
			NRWApplyMortgageVC *vc = [NRWApplyMortgageVC new];
			[self.navigationController pushViewController:vc animated:YES];
		}
	}
}

#pragma mark - getter
- (NSArray<NSArray <NSDictionary *>*> *)pageDataSource {
	
	if (_pageDataSource == nil) {
		_pageDataSource = @[
							@[
								@{@"imageName": @"financial_icon1",
								  @"title": @"申请抵押车辆"
								  }
								]
							];
	}
	return _pageDataSource;
}

- (NSArray<NSString *> *)pageHeaderDataSource {
	
	if (_pageHeaderDataSource == nil) {
		_pageHeaderDataSource = @[@"库存融资"];
	}
	return _pageHeaderDataSource;
}

@end
